<?php
namespace app\index\model;
use app\common\BaseModel;
use \think\Model;
use think\facade\Cookie;
use think\facade\Session;

class User extends BaseModel{
	 // 模型初始化
	
    protected static function init()
    {
    	self::$validate = new \app\index\validate\User;
        parent::init();
    }
  

	public function register(){
		$this->save(object_to_array(input()));
         return  self::$validate->getError();
	}
    const private_key = "-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALda8xLqttAs7FzD
AY5hLGhEi/VdGVSz4JbbVD/5J9ffGcyaHvaM5uJRRXbjciWufGn3X6eZ/ijs5lun
8S+OWQlE2/P70fFmyvVlVvdNp9IBS9RqeuwuQeF8AkdBB7FfVOl3jJRYtbE+S9VH
3vmE3LuY5IVIE02ptISIWFODZLK7AgMBAAECgYBDcqVwE/9j3OKlzOZJpd1cDbse
azziN7kqRUxcrB7H/j3TUe6J65MTNapD7a8TxXfm0yTQ1oj0LREgLks+nzKI4gNT
vr+sREfG07AbIwzJDLb7unzI0NlQ4hgKhiG+PliKvtaXh2MsYT2cR0GyvvFLwCrg
scYeV6pFuCHcaNdjAQJBAPFneePZWT0+zUGXdTqIxhjHHh8TYTIlvxY+hPF/jjHU
Pr6FEV4ap7hwyWJf3gD5TvdEgoBRR+gyDt6J6zhQIaECQQDCcPmrBwvomhkmxnGw
J3SjEhDhFTC0+7UHgG3C5HmoL1CNxmn7eAoytbygwAgDGiWN43Ptnb9FIn4iBDfX
rS7bAkB0FblkmxExxXB4uTeTRVBVe/L9KJmVvtcMAFZ3t0oOpizh+yVl5lV++SWu
W8FrIHgbqeNDh1KX0vKZiyMEumvhAkBvqjvn2XsgUOci2pJNzMryH5zr9qRrO6ai
MUJXy6iGSu3jDdCtpymMJ8y1MLMpPOnWS4q3KoG+7WkslemQBhhDAkEAoo05p4w9
9v55ybAaTcI/LLGGidLhSCSmzwFy2tP9F2H37cmR56VOSo96FvDn4VPiIfNNlv6r
L1zScatSN+tylQ==
-----END PRIVATE KEY-----
";
    /**
     * RSA解密方法
     * @param string $encrypted
     * @return string
     */
    public static function decrypt($encrypted) {
        $pi_key =  openssl_pkey_get_private(trim(self::private_key));

        $res = openssl_private_decrypt(base64_decode($encrypted),$decrypted,$pi_key);
       	if($res)
        	return $decrypted;
        return false;
    }

    public function login(){
       
       if(!geetest_check(input())){
            return  '滑块还没验证';
       }
            $info = '手机号或密码错误';
           if(self::$validate->checkAccount(input('mobile')) === true)
                return $info;
            $user = self::where('mobile', input('mobile'))->find();
            if(self::decrypt($user->password) !== self::decrypt(input('password')))
            	return $info;
            if(input('remember') === '1'){
                Session::set('login_token',md5(get_client_ip(0,true).input('mobile')));
            }

            return true;
        
        
    }
   
}
?>